﻿using System;


public enum FieldUIOutDateTimeStyle
{
    Full = 1,
    ShortDate = 2,
    LongDate = 3,
    ShortTime = 4,
    LongTime = 5
}

public static class FieldUI
{
    #region In Functions

    //bool
    public static bool? InBoolN(string val)
    {
        bool res;
        if (bool.TryParse(val, out res))
            return res;
        int? p = InIntN(val);
        return p == null ? null : (bool?)((int)p != 0);
    }

    public static bool InBool(string val)
    {
        bool? p = InBoolN(val);
        return p == null ? false : (bool)p;
    }

    //byte
    public static byte? InByteN(string val)
    {
        byte res;
        return byte.TryParse(val, out res) ? res : (byte?)null;
    }

    public static byte InByte(string val)
    {
        byte? p = InByteN(val);
        return p == null ? (byte)0 : (byte)p;
    }

    //int
    public static int? InIntN(string val)
    {
        int res;
        return int.TryParse(val, out res) ? res : (int?)null;
    }

    public static int InInt(string val)
    {
        int? p = InIntN(val);
        return p == null ? 0 : (int)p;
    }

    //char
    public static char? InCharN(string val)
    {
        char res;
        return char.TryParse(val, out res) ? res : (char?)null;
    }

    public static char InChar(string val)
    {
        char? p = InCharN(val);
        return p == null ? '\0' : (char)p;
    }

    //DateTime
    public static DateTime? InDateTimeN(string val)
    {
        DateTime res;
        return DateTime.TryParse(val, out res) ? res : (DateTime?)null;
    }

    public static DateTime InDateTime(string val)
    {
        DateTime? p = InDateTimeN(val);
        return p == null ? DateTime.MinValue : (DateTime)p;
    }

    //decimal
    public static decimal? InDecimalN(string val)
    {
        decimal res;
        return decimal.TryParse(val, out res) ? res : (decimal?)null;
    }

    public static decimal InDecimal(string val)
    {
        decimal? p = InDecimalN(val);
        return p == null ? (decimal)0 : (decimal)p;
    }

    //double
    public static double? InDoubleN(string val)
    {
        double res;
        return double.TryParse(val, out res) ? res : (double?)null;
    }

    public static double InDouble(string val)
    {
        double? p = InDoubleN(val);
        return p == null ? (double)0 : (double)p;
    }

    //short
    public static short? InShortN(string val)
    {
        short res;
        return short.TryParse(val, out res) ? res : (short?)null;
    }

    public static short InShort(string val)
    {
        short? p = InShortN(val);
        return p == null ? (short)0 : (short)p;
    }

    //long
    public static long? InLongN(string val)
    {
        long res;
        return long.TryParse(val, out res) ? res : (long?)null;
    }

    public static long InLong(string val)
    {
        long? p = InLongN(val);
        return p == null ? (long)0 : (long)p;
    }

    //sbyte
    public static sbyte? InSbyteN(string val)
    {
        sbyte res;
        return sbyte.TryParse(val, out res) ? res : (sbyte?)null;
    }

    public static sbyte InSbyte(string val)
    {
        sbyte? p = InSbyteN(val);
        return p == null ? (sbyte)0 : (sbyte)p;
    }

    //float
    public static float? InFloatN(string val)
    {
        float res;
        return float.TryParse(val, out res) ? res : (float?)null;
    }

    public static float InFloat(string val)
    {
        float? p = InFloatN(val);
        return p == null ? (float)0 : (float)p;
    }

    //string
    public static string InString(string val)
    {
        return val == null || val.Trim().Equals(string.Empty) ? null : val.Trim();
    }

    //ushort
    public static ushort? InUshortN(string val)
    {
        ushort res;
        return ushort.TryParse(val, out res) ? res : (ushort?)null;
    }

    public static ushort InUshort(string val)
    {
        ushort? p = InUshortN(val);
        return p == null ? (ushort)0 : (ushort)p;
    }

    //uint
    public static uint? InUintN(string val)
    {
        uint res;
        return uint.TryParse(val, out res) ? res : (uint?)null;
    }

    public static uint InUint(string val)
    {
        uint? p = InUintN(val);
        return p == null ? (uint)0 : (uint)p;
    }

    //ulong
    public static ulong? InUlongN(string val)
    {
        ulong res;
        return ulong.TryParse(val, out res) ? res : (ulong?)null;
    }

    public static ulong InUlong(string val)
    {
        ulong? p = InUlongN(val);
        return p == null ? 0 : (ulong)p;
    }
    #endregion

    #region Out Functions

    //bool
    public static string Out(bool? val, string nullStr, string trueStr, string falseStr)
    {
        return val == null ? nullStr : ((bool)val ? trueStr : falseStr);
    }

    public static string Out(bool val, string trueStr, string falseStr)
    {
        return val ? trueStr : falseStr;
    }

    //char
    public static string Out(char? val, string nullStr)
    {
        return val == null ? nullStr : val.ToString();
    }

    public static string Out(char val, string emptyCharStr)
    {
        return val.Equals('\0') ? emptyCharStr : val.ToString();
    }

    //byte
    public static string Out(byte? val, string nullStr)
    {
        return val == null ? nullStr : val.ToString();
    }

    public static string Out(byte val, string zeroStr)
    {
        return val == 0 ? zeroStr : val.ToString();
    }

    //DateTime
    public static string Out(DateTime? val, string nullStr, FieldUIOutDateTimeStyle style)
    {
        if (val == null)
            return nullStr;
        DateTime dt = (DateTime)val;
        switch (style)
        {
            case FieldUIOutDateTimeStyle.Full:
                return dt.ToString();
            case FieldUIOutDateTimeStyle.ShortDate:
                return dt.ToShortDateString();
            case FieldUIOutDateTimeStyle.LongDate:
                return dt.ToLongDateString();
            case FieldUIOutDateTimeStyle.ShortTime:
                return dt.ToShortTimeString();
            case FieldUIOutDateTimeStyle.LongTime:
                return dt.ToLongTimeString();
            default:
                return null;
        }
    }

    public static string Out(DateTime val, string minDateTimeStr,  FieldUIOutDateTimeStyle style)
    {
        if (val == DateTime.MinValue)
            return minDateTimeStr;
        switch (style)
        {
            case FieldUIOutDateTimeStyle.Full:
                return val.ToString();
            case FieldUIOutDateTimeStyle.ShortDate:
                return val.ToShortDateString();
            case FieldUIOutDateTimeStyle.LongDate:
                return val.ToLongDateString();
            case FieldUIOutDateTimeStyle.ShortTime:
                return val.ToShortTimeString();
            case FieldUIOutDateTimeStyle.LongTime:
                return val.ToLongTimeString();
            default:
                return null;
        }
    }

    //decimal
    public static string Out(decimal? val, string nullStr, int precision)
    {
        return val == null ? nullStr : (precision > 0 ? Math.Round((double)val, precision).ToString() : val.ToString());
    }

    public static string Out(decimal? val, string nullStr)
    {
        return Out(val, nullStr, -1);
    }

    public static string Out(decimal val, string zeroStr, int precision)
    {
        return val == 0 ? zeroStr : (precision > 0 ? Math.Round((double)val, precision).ToString() : val.ToString());
    }

    public static string Out(decimal val, string zeroStr)
    {
        return Out(val, zeroStr, -1);
    }

    //double
    public static string Out(double? val, string nullStr, int precision)
    {
        return val == null ? nullStr : (precision > 0 ? Math.Round((double)val, precision).ToString() : val.ToString());
    }

    public static string Out(double? val, string nullStr)
    {
        return Out(val, nullStr, -1);
    }

    public static string Out(double val, string zeroStr, int precision)
    {
        return val == 0 ? zeroStr : (precision > 0 ? Math.Round(val, precision).ToString() : val.ToString());
    }

    public static string Out(double val, string zeroStr)
    {
        return Out(val, zeroStr, -1);
    }

    //short
    public static string Out(short? val, string nullStr)
    {
        return val == null ? nullStr : val.ToString();
    }

    public static string Out(short val, string zeroStr)
    {
        return val == 0 ? zeroStr : val.ToString();
    }

    //int
    public static string Out(int? val, string nullStr)
    {
        return val == null ? nullStr : val.ToString();
    }

    public static string Out(int val, string zeroStr)
    {
        return val == 0 ? zeroStr : val.ToString();
    }

    //long
    public static string Out(long? val, string nullStr)
    {
        return val == null ? nullStr : val.ToString();
    }

    public static string Out(long val, string zeroStr)
    {
        return val == 0 ? zeroStr : val.ToString();
    }

    //sbyte
    public static string Out(sbyte? val, string nullStr)
    {
        return val == null ? nullStr : val.ToString();
    }

    public static string Out(sbyte val, string zeroStr)
    {
        return val == 0 ? zeroStr : val.ToString();
    }

    //float
    public static string Out(float? val, string nullStr, int precision)
    {
        return val == null ? nullStr : (precision > 0 ? Math.Round((double)val, precision).ToString() : val.ToString());
    }

    public static string Out(float? val, string nullStr)
    {
        return Out(val, nullStr, -1);
    }

    public static string Out(float val, string zeroStr, int precision)
    {
        return val == 0 ? zeroStr : (precision > 0 ? Math.Round(val, precision).ToString() : val.ToString());
    }

    public static string Out(float val, string zeroStr)
    {
        return Out(val, zeroStr, -1);
    }

    //string
    public static string Out(string str, string nullStr)
    {
        return str == null ? nullStr : str;
    }

    //ushort
    public static string Out(ushort? val, string nullStr)
    {
        return val == null ? nullStr : val.ToString();
    }

    public static string Out(ushort val, string zeroStr)
    {
        return val == 0 ? zeroStr : val.ToString();
    }

    //uint
    public static string Out(uint? val, string nullStr)
    {
        return val == null ? nullStr : val.ToString();
    }

    public static string Out(uint val, string zeroStr)
    {
        return val == 0 ? zeroStr : val.ToString();
    }

    //ulong
    public static string Out(ulong? val, string nullStr)
    {
        return val == null ? nullStr : val.ToString();
    }

    public static string Out(ulong val, string zeroStr)
    {
        return val == 0 ? zeroStr : val.ToString();
    }
    #endregion
}
